package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.entity.SdkVideoBaidu;
import com.lebang.example.PointwallRecordExample;
import com.lebang.service.PointWallSdkService;
import com.lebang.service.SdkVideoService;
import com.lebangweb.common.TextUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: hwq
 * @Date: 2019/10/15 11:01
 * @Version 1.0
 */
@RequestMapping("/sdk")
@Controller
public class SdkController {
    @Autowired
    private PointWallSdkService xianWanService;
    @Autowired
    private SdkVideoService sdkVideoService;

    @GetMapping("/list")
    @Login
    public String list() {
        return "/sdk/list";
    }

    @Login
    @PostMapping("/list")
    @ResponseBody
    public Map list(HttpServletRequest request, Long page, Integer limit, String sdkId, String star, String end) {
        Map map = new HashMap(1);
        PointwallRecordExample example = new PointwallRecordExample();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        PointwallRecordExample.Criteria criteria = example.createCriteria();
        if (!TextUtil.isEmpty(sdkId)) {
            criteria.andSdkIdEqualTo(sdkId);
        }
        if (!TextUtil.isEmpty(star, end)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                criteria.andCreateTimeBetween(formatter.parse(star), formatter.parse(end));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        map.put("count", xianWanService.countByExample(example));
        map.put("code", 0);
        map.put("data", xianWanService.selectByExample(example));
        return map;
    }

    @Login
    @GetMapping("/videolist")
    public String videolist() {
        return "/sdk/baidu";
    }
    @Login
    @PostMapping("/videolist")
    @ResponseBody
    public JSONObject videolist(HttpServletRequest request, Long page, Integer limit, String sdkId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) @ApiParam(value = "开始日期 例子: 2019-10-26 11:12:13") Date star,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) @ApiParam(value = "结束日期 例子: 2019-10-26 11:12:13") Date end
    ) {
        JSONObject map = new JSONObject(1);
        page=(page-1)*limit;
        Page<SdkVideoBaidu> data=sdkVideoService.pageByExample(sdkId, page, limit,  star, end);
        map.put("count", data.getTotalElements());
        map.put("code", 0);
        map.put("data", data.get());
        return map;
    }
}
